package router

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"role-mgr1/handler"
)

func Cors() gin.HandlerFunc {

	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", "*")
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
			c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
			c.Header("Access-Control-Allow-Credentials", "true")
			c.Set("content-type", "application/json")
		}
		//放行所有OPTIONS方法
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}
func Init() *gin.Engine {
	// 1.创建路由
	r := gin.Default()
	r.Use(Cors())
	r.Static("/static", "./static")
	r.POST("/login", handler.Login)
	r.POST("/userRole/add", handler.UserRoleAdd)
	r.GET("/userRole/list", handler.UserRoleList)
	r.POST("/roleNode/add", handler.RoleNodeAdd)
	r.GET("/roleNode/list", handler.RoleNodeList)
	r.POST("car/add", handler.CarAdd)
	r.POST("order/add", handler.OrderAdd)
	r.GET("order/list", handler.CarOrderList)
	r.GET("order/detail", handler.CarOrderDetail)
	r.GET("/user/list", handler.UserList)
	r.GET("/user/detail", handler.UserDetail)
	r.POST("/repo/add", handler.RepoAdd)
	r.POST("/repoOrder/add", handler.RepoOrderAdd)
	r.GET("/repoOrderList", handler.RepoOrderList)
	r.GET("/repoOrderDetail", handler.RepoOrderDetail)
	return r
}
